package com.liu.service.impl;
import com.alibaba.fastjson.JSON;
import com.liu.config.RedisConfig;
import com.liu.dao.TicketsDao;
import com.liu.entity.Tickets;
import com.liu.service.intf.TicketsService;
import com.liu.utils.JedisUtils;
import com.liu.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class TicketsServiceImpl implements TicketsService {
    //实例化日志对象
    private Logger logger = LoggerFactory.getLogger(TicketsServiceImpl.class);
    @Resource
    private TicketsDao dao;

    //    查询列表
    @Override
    public R querylist() {
        List<Tickets> list;
       // list = dao.selectlist();
       // 1、查缓存(默认以综合条件查询
        if (JedisUtils.esists(RedisConfig.TICKETS_ATTRACTIONS)) {
            System.out.println("走缓存");
           list = JSON.parseArray(JedisUtils.getStr(RedisConfig.TICKETS_ATTRACTIONS), Tickets.class);
        } else {
//  缓存中没有，则查数据库
            System.out.println("不走缓存");
            list = dao.selectlist();
            JedisUtils.setStr(RedisConfig.TICKETS_ATTRACTIONS, JSON.toJSONString(list));
        }
        return R.ok(list);
    }

    @Override
    public R save(Tickets tickets) {
        //1.验证
        if (tickets != null) {
            tickets.setCtime(new Date());
            //2.操作数据库
            if (dao.insert(tickets) > 0) {
                return R.ok();
            }
        }
        return R.fail("网络异常！");
    }


    @Override
    public R queryDeatil(int id) {
        //1.验证参数
        if (id > 0) {
            Tickets tickets = dao.selectById(id);
            return R.ok(tickets);
        }

        return R.fail("网络异常！");
    }

}


